﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reactive.Linq;
using System.Threading;
using System.Net;
using System.IO;

namespace ConsoleApp
{
    public class demo_rx
    {
        public static void demo()
        {
            Console.WriteLine("--using range return the observable collection--");
            IObservable<int> input = Observable.Range(1, 15);
            input.Where(i => i % 2 == 0).Subscribe(x => Console.WriteLine("{0} ", x));


            Console.WriteLine("\n--using array return the observable collection--");
            var myArray = new[] { 1, 2, 1, 3, 2, 4, 5, 6, 7, 8, 9, 10 };
            IObservable<int> arrayObservable = myArray.ToObservable();
            arrayObservable.Distinct().Skip(2).Take(5).Select(x => x * x).Subscribe(x => Console.WriteLine("{0} ", x));

            string[] codes = { "AL", "AK", "AZ", "AR" };
            string[] states = { "Alabama", "Alaska", "Arizona", "Arkansas", "California" };
            var CodesWithStates = codes.Zip(states, (code, state) => code + ": " + state);
            foreach (var item in CodesWithStates)
            {
                Console.WriteLine(item);
            }
            /*
            AL: Alabama
            AK: Alaska
            AZ: Arizona
            AR: Arkansas
             */

            //testPlusTwoNumber();

            //testWeb();          
        }

        #region call a method when the method maybe more time using async and sync two mode
        private static void testPlusTwoNumber()
        {
            Console.WriteLine("\n--compare capture add two num in async and sync mode--");

            Console.WriteLine("sync: " + plusTwoNumber(1, 2));
            Console.WriteLine("sync completed");

            plusTwoNumberStartAsync(1, 2).Subscribe(x => Console.WriteLine("async: {0} ", x));
            Console.WriteLine("async completed");
        }

        private static IObservable<int> plusTwoNumberStartAsync(int x, int y)
        {
            /* need to check the remark whether will block or not */

            //return Observable.Start(() => plusTwoNumber(2, 3)); //not block
            return Observable.Return(plusTwoNumber(x, y));        // it will block the current thread
        }

        private static int plusTwoNumber(int x, int y)
        {
            Thread.Sleep(1000);
            return x + y;
        }
        #endregion

        #region call a method to dispaly the web content
        private static void testWeb()
        {
            web().Select(res =>
            {
                using (var stream = res.GetResponseStream())
                using (var sr = new StreamReader(stream))
                {
                    return sr.ReadToEnd();
                }
            }).Subscribe(x => Console.WriteLine("{0} ", x));
        }

        private static IObservable<WebResponse> web(string url = "http://www.baidu.com")
        {
            WebRequest request = WebRequest.Create(url);

            return Observable.FromAsyncPattern<WebResponse>(request.BeginGetResponse, request.EndGetResponse).Invoke();
        }
        #endregion
    }
}
